<?php

class Log_Adapter_Database extends Log_Adapter
{

	protected $_name;

	public function __construct($name, $config = null)
	{
		$name = isset($config['name']) ? $config['name'] : null;

		if (null == $name)
		{
			$name = 'core_log';
		}

		$this -> _name = Model::getTableName($name);
	}

	public function write( $message,$level = null)
	{
		if(null == $level){
			$level =  Logger::INFO;
		}
		$db = Database::instance();
		$db -> insert($this -> _name, array(
			'message' => (string)$message,
			'level' => $level,
			'timestamp' => date('Y-m-d H:i:s', time()),
			'request_ip' => Request::getClientIp(),
		));
		return $this;
	}

}
